#include "widget.h"
#include "ui_widget.h"
#include "qmessagebox.h"


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //
    mqttClient = new QMqttClient();
    mqttClient->setHostname("8.146.206.1");
    mqttClient->setPort(1883);
    mqttClient->setUsername("App_Username");
    mqttClient->setPassword("App_Password");
    mqttClient->setClientId("App_ClientId");

    //mqtt信号与槽的绑定函数
    connect(mqttClient, &QMqttClient::connected, this, &Widget::mqttConnected);
    connect(mqttClient, &QMqttClient::messageReceived, this, &Widget::mqttMsgRec);

    //
    mqttClient->connectToHost();
    //ui 组件的初始化
    ui->tableWidget->setColumnWidth(0, 300);
    //
//    waveChartWidget = new WaveChart(this);
//    waveChartWidget->show();
//    qDebug() << (waveChartWidget==nullptr);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::mqttConnected()
{
    //订阅消息
    mqttClient->subscribe(QString(TOPIC_DATA_WAVE), 0);
    mqttClient->subscribe(QString(TOPIC_DATA_DEVICEID), 0);
}

void Widget::mqttMsgRec(const QByteArray &message, const QMqttTopicName &topic)
{
    if(topic.name() == TOPIC_DATA_WAVE){//波形数据
        qDebug() << "收到: 波形数据";
        //解析数据
        QJsonDocument doc = QJsonDocument::fromJson(message);
        QJsonObject obj = doc.object();
        //
        QString deviceid = obj["deviceid"].toString();
        QString takeTime = obj["takeTime"].toString();
        int takeMs = obj["takeMs"].toInt();
        int maxValue = obj["maxValue"].toInt();
        int minValue = obj["minValue"].toInt();
        int avgValue = obj["avgValue"].toInt();
        int dataLen = obj["dataLen"].toInt();
        QJsonArray arr = obj["datas"].toArray();
        qDebug() << "takeMs = " << takeMs << ",deviceid="<<deviceid<<",maxValue="<<maxValue;
        qDebug() << "minValue = " << minValue << ",avgValue="<<avgValue<<",dataLen="<<dataLen;

        //添加到内存中
        int rowCnt = ui->tableWidget->rowCount();
        qDebug() << "rowCnt=[" << rowCnt << "] ";


        ui->tableWidget->insertRow(rowCnt);
        ui->tableWidget->setItem(rowCnt, 0, new QTableWidgetItem(takeTime));
        ui->tableWidget->item(rowCnt, 0)->setTextAlignment(Qt::AlignCenter);
        //
        ui->tableWidget->setItem(rowCnt, 1, new QTableWidgetItem(QString::number(takeMs)));
        ui->tableWidget->item(rowCnt, 1)->setTextAlignment(Qt::AlignCenter);
        //
        ui->tableWidget->setItem(rowCnt, 2, new QTableWidgetItem(deviceid));
        ui->tableWidget->item(rowCnt, 2)->setTextAlignment(Qt::AlignCenter);
        //
        ui->tableWidget->setItem(rowCnt, 3, new QTableWidgetItem(QString::number(avgValue)));
        ui->tableWidget->item(rowCnt, 3)->setTextAlignment(Qt::AlignCenter);
        //
        ui->tableWidget->setItem(rowCnt, 4, new QTableWidgetItem(QString::number(maxValue)));
        ui->tableWidget->item(rowCnt, 4)->setTextAlignment(Qt::AlignCenter);
        //
        ui->tableWidget->setItem(rowCnt, 5, new QTableWidgetItem(QString::number(minValue)));
        ui->tableWidget->item(rowCnt, 5)->setTextAlignment(Qt::AlignCenter);
        //
        ui->tableWidget->setItem(rowCnt, 6, new QTableWidgetItem(QString::number(dataLen)));
        ui->tableWidget->item(rowCnt, 6)->setTextAlignment(Qt::AlignCenter);
        //
        ui->tableWidget->setItem(rowCnt, 7, new QTableWidgetItem("展示波形"));
        ui->tableWidget->item(rowCnt, 7)->setTextAlignment(Qt::AlignCenter);

        dataVec.append(arr);
        return;
    }

    if(topic.name() == TOPIC_DATA_DEVICEID){//装置在线列表
        qDebug() << "收到: 在线装置列表";
        //解析数据
        QJsonDocument doc = QJsonDocument::fromJson(message);
        QJsonObject obj = doc.object();
        QString deviceid = obj["deviceid"].toString();
        qDebug() << "deviceid=" << deviceid;
        if(!deviceVec.contains(deviceid)){
            deviceVec.append(deviceid);
            //刷新页面:在combobox中展示数据
            ui->comboBox->addItem(deviceid);
        }
        return;
    }

    qDebug() << "收到: 不支持的主题类型["<< topic.name()<< "]";
}


void Widget::on_pushButton_clicked()
{
    if(mqttClient->state() != QMqttClient::Connected) {
        QMessageBox::information(this, "提示", "mqtt连接建立失败!");
        return;
    }
    //发布主题消息
    QJsonObject obj;
    QJsonDocument doc(obj);
    mqttClient->publish(QMqttTopicName(TOPIC_CMD_WAVE), doc.toJson());
    qDebug() << "主题[" << TOPIC_CMD_WAVE << "]已经发布";
}

void Widget::on_pushButton_2_clicked()
{
    if(mqttClient->state() != QMqttClient::Connected) {
        QMessageBox::information(this, "提示", "mqtt连接建立失败！");
        return;
    }
    //发布主题消息
    QJsonObject obj;
    obj["deviceid"]=ui->comboBox->currentText();
    QJsonDocument doc(obj);

    mqttClient->publish(QMqttTopicName(TOPIC_CMD_DEVICEID), doc.toJson());
    qDebug() << "主题[" << TOPIC_CMD_DEVICEID << "]已经发布";
}

void Widget::on_tableWidget_cellDoubleClicked(int row, int column)
{
    //表格的单元格的双击事件
    if(waveChartWidget == nullptr){
        waveChartWidget = new WaveChart();
    }

    waveChartWidget->show(dataVec[row]);
}

